from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse, Http404
from django.core.files.uploadedfile import InMemoryUploadedFile
# from asset.models import DomainList
import whois
import nmap
from urllib import request as r
import os
import subprocess
import requests

headers = {
    'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  r'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3'
}

# SQL注入检测
# def sql_test(request, action=None):
#     if request.method == 'POST':
#         target = request.POST.get('arg')
#         print(target)
#         res = whois.whois(target)
#         result = request.GET.get('result')
#         result = res
#         return render(request, 'info/who_is.html', {'result': result})
#
#     else:
#         return render(request, 'bugtest/sql_test.html')
        # return HttpResponse('fdgd灌灌灌灌灌灌灌灌灌灌灌灌 g')
        # return render(request, 'info/port_scan.html')


def jiechi(request, action=None):
    if request.method == 'POST':
        url = request.POST.get('arg')
        print(url)
        urls = "{}".format(url)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
        reqst = requests.get(url=urls, headers=headers, timeout=6)
        if reqst.status_code == 200:
            print('[+]Target stability')
            try:
                hea = reqst.headers
                print(hea['X-FRAME-OPTIONS'])
                print('[n]X-FRAME-OPTIONS header, no click hijacking vulnerability can be used')
            except:
                print('[+]There is no X-FRAME-OPTIONS header. There is a clicking hijacking vulnerability')
                print('[!]Remind:It may be a defense against JS. Please test it yourself')
                result1 = '[+]There is no X-FRAME-OPTIONS header. There is a clicking hijacking vulnerability '
                result2 = '[!]Remind:It may be a defense against JS. Please test it yourself '
        else:
            print('[n]bjective instability')
            result1 = '[n]bjective instability'
        return render(request, 'bugtest/jiechi.html', {'result1': result1,'result2': result2})
    else:
        return render(request, 'bugtest/jiechi.html')
        return HttpResponse('frrrrrrrrrrrrr')
        # return render(request, 'info/port_scan.html')
